#include "window.h"
#include "cubicItem.h"
#include "data.h"
#include "grab_widget.h"
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QLayout>
#include <QWidget>
#include <QKeyEvent>
#include <QMessageBox>
#include <QTimer>
#include <QPixmap>
#include <QIcon>

extern int data_src[4][4];
window::window()
{
    this->setAutoFillBackground(true);
    mins = 0;
    secs = 0;
    this->setMinimumSize(334,410);
    this->setMaximumSize(334,410);

    QGridLayout *game_layout =new QGridLayout;
    int i,j;
    for (i = 0;i < 4;i++)
    {
        for (j = 0;j < 4;j++)
        {
            this->cubic_matrix[i][j] = new cubicItem;
            game_layout->addWidget(cubic_matrix[i][j],i,j);
        }
    }
    this->update_data();
    game_layout->setSizeConstraint(QLayout::SetFixedSize);
    QWidget *game_field = new QWidget;
    game_field->setLayout(game_layout);

    QGridLayout *fun_layout = new QGridLayout;
     time_label = new QLabel("00:00");
   \
     QPixmap up_pix(":/icon/icon/up.png");
     QIcon down_icon(":/icon/icon/down.png");
     QIcon left_icon(":/icon/icon/left.png");
     QIcon right_icon(":/icon/icon/right.png");
     this->up_button = new QPushButton;
     up_button->setIcon(up_pix);
     up_button->setFixedSize(up_pix.width(),up_pix.height());
     this->down_button = new QPushButton(down_icon,"");
     this->left_button = new QPushButton(left_icon,"");
     this->right_button = new QPushButton(right_icon,"");
     down_button->setFixedSize(up_pix.width(),up_pix.height());
     right_button->setFixedSize(up_pix.width(),up_pix.height());
     left_button->setFixedSize(up_pix.width(),up_pix.height());
     up_button->setAutoDefault(false);
     down_button->setAutoDefault(false);
     left_button->setAutoDefault(false);
     right_button->setAutoDefault(false);

     QAction *quit_act = new QAction(QIcon(":/icon/icon/quit.png"),tr("退出"),this);
     QAction *retry_act = new QAction(QIcon(":icon/icon/retry.png"),tr("重新再来"),this);
     QAction *grab_act = new QAction(QIcon(":icon/icon/grab.png"),tr("截图"),this);
     connect(grab_act,SIGNAL(triggered()),this,SLOT(slot_grab()));
    connect(quit_act,SIGNAL(triggered()),this,SLOT(slot_quit()));
    connect(retry_act,SIGNAL(triggered()),this,SLOT(slot_retry()));
    QMenu *game_menu = menuBar()->addMenu(tr("游戏"));
    game_menu->addAction(retry_act);
    game_menu->addAction(quit_act);
    game_menu->addAction(grab_act);

     retry_button = new QPushButton(QIcon(":/icon/icon/retry.png"),tr("重新再来"));
     quit_button = new QPushButton(QIcon(":/icon/icon/quit.png"),tr("退出"));
     retry_button->setStyleSheet("QPushButton{border-width:0px}");
     connect(retry_button,SIGNAL(clicked()),this,SLOT(slot_retry()));
     connect(quit_button,SIGNAL(clicked()),this,SLOT(slot_quit()));

    fun_layout->addWidget(up_button,0,2);
    fun_layout->addWidget(retry_button,1,0);
    fun_layout->addWidget(left_button,1,1);
    fun_layout->addWidget(time_label,1,2);
    fun_layout->addWidget(right_button,1,3);
    fun_layout->addWidget(quit_button,1,4);
    fun_layout->addWidget(down_button,2,2);
    game_layout->addLayout(fun_layout,4,0,3,4);

    this->game_timer = new QTimer;
    connect(game_timer,SIGNAL(timeout()),this,SLOT(slot_time_update()));


    this->time_label->setText("00:00");
    this->setFocus();
    this->setCentralWidget(game_field);
}
window::~window()
{

}
void window::keyPressEvent(QKeyEvent *event)
{
    if (game_is_over())
    {
        QMessageBox::information(this,"游戏结束","游戏结束");
    }
    switch (event->key())
    {
    case (Qt::Key_Down):
        down();
        break;
    case (Qt::Key_Up):
        up();
        break;
    case(Qt::Key_Left):
        left();
        break;
    case(Qt::Key_Right):
        right();
        break;
    default:
        return;
    }
    if (!this->game_timer->isActive())
        this->game_timer->start(1000);
    create_new_data();
    this->update_data();
    if (game_is_over())
    {
        QMessageBox::information(this,"游戏结束","游戏结束");
    }
}
void window::update_data()
{
    int i,j;
    for (i = 0;i < 4;i++)
        for (j = 0;j < 4;j++)
        {
            this->cubic_matrix[i][j]->set_value(data_src[i][j]);
        }
}
void window::slot_time_update()
{
    this->secs++;
    QString tmp1,tmp2,time;
    if (secs < 10)
        tmp1 = "0" + QString::number(secs);
    else if (secs > 59)
    {
        mins++;secs = 0;
        tmp1 = QString::number(secs);
    }
    else tmp1 = QString::number(secs);
    if (mins < 10)
        tmp2 = "0" + QString::number(mins);
    else if (mins < 100)
    {
        tmp2 = QString::number(mins);
    }
    time = tmp2 + ":" + tmp1;
    this->time_label->setText(time);
}
void window::slot_quit()
{
    this->close();
}
void window::slot_retry()
{
    this->game_timer->stop();
    this->time_label->setText("00:00");
    data_init();
    this->update_data();
    this->setFocus();
}
void window::slot_grab()
{
    grab_widget *image = new grab_widget(this->grab(QRect(0,0,334,290)),this);
    image->show();
}
